document.addEventListener('DOMContentLoaded', function() {
  var likesbtn = document.getElementById('likes-btn')
  var alllikes = document.getElementById('alllikes')
  likesbtn.addEventListener('click', function() {
    if(likesbtn.className !== 'fa' + " " + "fa-heart") {
      fetch(
        '/images/likes/' + likesbtn.dataset.imageid,
        {method: 'POST', body: {}}
      ).then(response => response.json())
      .then(resp => {
          console.log(resp)   
          likesbtn.className = "fa"
          likesbtn.className += " " + "fa-heart"
          likesbtn.innerHTML = resp.likes
          alllikes.innerHTML = parseInt(alllikes.innerHTML) + 1
      })
    }
  })
})

window.onload=function(){
  var visitsnum = document.getElementById('visitsnum')
  fetch(
    '/images/visit/' + visitsnum.dataset.imageid,
    {method: 'POST', body: {}}
  ).then(response => response.json())
  .then(resp => {  
      visitsnum.innerHTML = resp.visit;
  })
}